﻿Imports System.Data.SqlClient
Public Class frmBuscarOrdenes
    Private mstrCondicion As String
    Private mstrValor As String
    Dim mdsDatos As New DataSet
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property
    Private Sub frmBuscarOrdenes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtBuscar.Clear()
        dgBuscar.Rows.Clear()

        ActualizarTodas()
    End Sub
    Sub ActualizarTodas()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBuscar.Rows.Clear()

        Dim strNombre As String
        Dim cnconn As New SqlConnection
        Dim strSql As String = ""
        Dim cmdBuscar As New SqlCommand
        Dim cmdAsignar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim fecha As Date
        Dim Status As String

        Try
            strNombre = UCase(txtBuscar.Text.Trim)
            strNombre = "%" & strNombre.Replace(" ", "%") & "%"

            cnconn.ConnectionString = CitraConnection
            cnconn.Open()

            cmdBuscar.Connection = cnconn
            cmdAsignar.Connection = cnconn

            strSql = "Select IdMantenimiento,Fecha,NoEconomico,NombreRuta,NombreMecanico,Status " & _
                     "From Mantenimientos " & _
                     "WHERE upper(NoEconomico) Like '" & strNombre & "' " & mstrCondicion & " OR upper(NombreMecanico) like '" & strNombre & "' " & mstrCondicion & " Order By fecha DESC"

            cmdBuscar.CommandText = strSql
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                fecha = rdBuscar("fecha")
                If rdBuscar("Status") = 0 Then
                    Status = "Activa"
                Else
                    Status = "Cancelada"
                End If
                dgBuscar.Rows.Add(rdBuscar("IdMantenimiento"), rdBuscar("NoEconomico").ToString.Trim, rdBuscar("NombreRuta").ToString.Trim, fecha.ToShortDateString, rdBuscar("NombreMecanico"), Status)

            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try





    End Sub
    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        ActualizarTodas()
    End Sub

    Private Sub RadButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton2.Click
        Dim strRetVal As String = ""

        Try
            strRetVal = CStr(dgBuscar.Item(0, dgBuscar.CurrentCell.RowIndex).Value)
        Catch ex As Exception
            strRetVal = ""
        Finally
            mstrValor = strRetVal.Trim
        End Try

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
    Private Sub dgResult_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgBuscar.CellDoubleClick

        RadButton2_Click(Me, System.EventArgs.Empty)

    End Sub

    Private Sub dgResult_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgBuscar.KeyDown

        If e.KeyCode = Keys.Enter Then
            RadButton2_Click(Me, System.EventArgs.Empty)
        End If

    End Sub
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class